#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "NetworkServer.h"

using namespace Marco;

NetworkServer::NetworkServer(int port)
{
   mSocket = -1;
   mPort = port;
}

NetworkServer::~NetworkServer()
{
   if(mSocket > 0)
   {
      close(mSocket);
   }
}

void NetworkServer::Start()
{
   mSocket = socket(AF_INET, SOCK_STREAM, 0);
   if(mSocket < 0)
   {
      std::cout << "Error, failed to open port: " << mPort << std::endl;
      exit(1);
   }

   bzero(&mServerAddress, sizeof(mServerAddress));

   mServerAddress.sin_family = AF_INET;
   mServerAddress.sin_addr.s_addr = INADDR_ANY;
   mServerAddress.sin_port = htons(mPort);

   if(bind(mSocket, (struct sockaddr *) &mServerAddress, sizeof(mServerAddress)) < 0)
   {
      std::cout << "Failed to bind to port: " << mPort << std::endl;
      exit(0);
   }
}

void NetworkServer::Listen()
{
   std::cout << "Listening for client connection." << std::endl;
   listen(mSocket, 5);

   socklen_t clen = sizeof(mClientAddress);
   mClient = accept(mSocket, (struct sockaddr *) &mClientAddress, &clen);
   if(mClient < 0)
   {
      std::cout << "Error, cannot accept client." << std::endl;
      exit(1);
   }
}

Event NetworkServer::Read()
{
   Event event;
   //while(true)
   {
      //listen(mSocket, 5);

      //socklen_t clen = sizeof(mClientAddress);
      //mClient = accept(mSocket, (struct sockaddr *) &mClientAddress, &clen);
      //if(mClient < 0)
      //{
      //   std::cout << "Error, cannot accept client." << std::endl;
      //}

      char msg[17] = { 0 };
      int count = 0;
      //while((count = read(mClient, msg, sizeof(Event))) >= 0)
      {
         count = read(mClient, msg, sizeof(msg));
         if(count <= 0)
         {
            close(mClient);
            close(mSocket);
            mSocket = -1;
            throw(std::string("Bad client socket read."));
         }
         std::cout << "NetworkServer::ClientSocket() received: " << count << " bytes, size of Event = " << sizeof(msg) << std::endl;
         std::cout << sizeof(char) << ", " << sizeof(int) << ", " << sizeof(float) << std::endl;

         bzero(&event, sizeof(event));
         event.Source = msg[0];
         int i;
         memcpy(&i, &msg[1], 4);
         event.KeyCode = ntohl(i);
         memcpy(&i, &msg[5], 4);
         event.KeyEvent = ntohl(i);

         float f;
         memcpy(&f, &msg[9], 4);
         event.Dx = ntohf(f);
         memcpy(&f, &msg[13], 4);
         event.Dy = ntohf(f);

         std::cout << "Code = " << event.KeyCode << ", Event = " << event.KeyEvent << std::endl;
         std::cout << "Dx = " << event.Dx << ", Dy = " << event.Dy << std::endl;
      }

      //close(mClient);
      //break;
   }
   //close(mSocket);
   //mSocket = -1;

   return(event);
}

float NetworkServer::ntohf( const float inFloat )
{
   float retVal;
   char *floatToConvert = ( char* ) & inFloat;
   char *returnFloat = ( char* ) & retVal;

   // swap the bytes into a temporary buffer
   returnFloat[0] = floatToConvert[3];
   returnFloat[1] = floatToConvert[2];
   returnFloat[2] = floatToConvert[1];
   returnFloat[3] = floatToConvert[0];

   return retVal;
}
